package com.winchain.chainbill.bill.biz.send.cmd;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.framework.command.CallbackContext;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICallback;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.MessageSender;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.message.keyword.RcvRevKeyword;
import com.shell.governor.modules.message.keyword.SysSndRevRejectKeyword;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.biz.Bill;
import org.springframework.stereotype.Component;

@Component
public class BillSndMsgCmd implements ICommand<Bill<CbSnd>>, ICallback<Bill<CbSnd>, AcctTxRec> {
	public boolean execute(CommandContext<Bill<CbSnd>> context) throws Exception {
		CbRec cbRec = ((Bill) context.getSource()).getRec();
		CbSnd cbSnd = (CbSnd) ((Bill) context.getSource()).getSource();
		CbOpt cbOpt = (CbOpt) context.getValue("Operation");

		String bizType = (String) context.getValue("bizType");
		if (("cb_snd".equals(bizType)) && ("EX02".equals(cbOpt.getOptCode()))) {
			if (SysDateUtil.compare(cbRec.getSndDate(), SysDateUtil.getSystemDate()) <= 0) {
				String template = "MT01301";
				RcvRevKeyword keyWord = new RcvRevKeyword();
				keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
				keyWord.setExpDate(DateUtil.parseDate(cbRec.getExpDate(), "yyyyMMdd", "yyyy-MM-dd"));
				keyWord.setBuyerNm(cbSnd.getBuyerNm());
				keyWord.setSndDate(DateUtil.parseDate(cbSnd.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
				keyWord.setUrl(SpringContextUtil.getProperty("chainbill.website"));

				String senderId = CommonDataUtil.getUserId();
				String rcvId = cbSnd.getSellerId();

				MessageSender.sendMessageByTmpl(senderId, template, keyWord, rcvId);
				template = "MT02201";

				MessageSender.sendMessageByTmpl(senderId, template, keyWord, rcvId);
			}
		} else if ("SND_REJECT".equals(bizType)) {
			String template = "MT01203";
			SysSndRevRejectKeyword keyWord = new SysSndRevRejectKeyword();
			keyWord.setBillAmt(String.valueOf(cbRec.getBillAmt()));
			keyWord.setSellerNm(cbSnd.getSellerNm());
			keyWord.setSndDate(DateUtil.parseDate(cbSnd.getCreateTime(), "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));

			keyWord.setContactorName(CommonDataUtil.getUserName());
			keyWord.setRemark(cbOpt.getRemark());

			String senderId = CommonDataUtil.getUserId();

			MessageSender.sendMessageByTmpl(senderId, template, keyWord, cbSnd.getBuyerId());
		}
		return false;
	}

	public boolean callback(CallbackContext<Bill<CbSnd>, AcctTxRec> context) throws Exception {
		execute(context);
		return false;
	}
}
